﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace pinduoduo.Util
{
    /// <summary>
    /// 深复制扩展类，不需要T支持可序列化
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DeepCopyExtensions<T>
    {

        public static T DeepCopy(T item)
        {
            MemoryStream stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(typeof(T));
            xml.Serialize(stream, item);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)xml.Deserialize(stream);
        }

        public static List<T> DeepCopy(object list)
        {
            MemoryStream stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(typeof(List<T>));
            xml.Serialize(stream, list);
            stream.Seek(0, SeekOrigin.Begin);
            return (List<T>)xml.Deserialize(stream);
        }
    }
}
